Ext.define('CS.data.reader.Tree', {
	extend: 'Ext.data.reader.Json',

	childrenPro: 'children',

	getResponseData: function(response) {
		var me = this, data, error;

		try {
			data = Ext.decode(response.responseText);

			if (CS.isNotEmpty(data.data)) {
				me.loopInit(data.data);
			}

			return this.readRecords(data);
		} catch (ex) {
			error = new Ext.data.ResultSet({
				total: 0,
				count: 0,
				records: [],
				success: false,
				message: ex.message
			});

			this.fireEvent('exception', this, response, error);

			Ext.Logger.warn('Unable to parse the JSON returned by the server');

			return error;
		}
	},

	loopInit: function(data) {
		var me = this;

		Ext.each(data, function(one) {
			one.leaf = CS.getBoolean(one.leaf);
			one.expanded = CS.getBoolean(one.expanded);

			if (me.needCheck) {
				one.checked = CS.getBoolean(one.checked);
			} else {
				delete one.checked;
			}

			var children = one[me.childrenPro];
			if (CS.isNotEmpty(children)) {
				me.loopInit(children);
			}
		});
	}

});
